package com.antball.aaa.command_responsibilitychain_design;

import com.antball.aaa.command_responsibilitychain_design.model.CommandVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: huangsj
 * @Date: 2019/8/6 17:41
 * @Description:
 */
public abstract class Command {

    /**
     * 执行命令
     * @param commandVo
     * @return
     */
    public abstract String execute(CommandVo commandVo);


    /**
     * 创建责任链相关的类
     * @param baseCommandName
     * @return
     */
    protected  final List<? extends CommandName> buildChain(Class<? extends CommandName> baseCommandName){
        List<Class> classes = new ArrayList<Class>();
        classes.add(Ls.class);
        classes.add(Ls_a.class);
        classes.add(Ls_l.class);

        //todo 取出基类的所有实现的类

        ArrayList<CommandName> commandNameList = new ArrayList<CommandName>();
        for (Class c: classes){
            CommandName commandName=null;

            try {
                commandName = (CommandName)Class.forName(c.getName()).newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(commandNameList.size()>0){
                    commandNameList.get(commandNameList.size()-1).setNext(commandName);
            }
            commandNameList.add(commandName);
        }

        return commandNameList;
    }
}
